
const path = require('node:path')
const nodeExternals = require('webpack-node-externals')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const { DefinePlugin } = require('webpack')

module.exports = {
  entry: {
    app: './bin/www'
  },
  devtool: 'eval-cheap-module-source-map',
  externals: [nodeExternals()],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].[contenthash:8].bundle.js',
    clean: true
  },
  context: path.resolve(__dirname, './'),
  target: 'node',
  mode: 'production',
  module: {
   rules:[
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      }
   ] 
  },
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, './public'),
          to: path.resolve(__dirname, './dist/public')
        },
        {
          from: path.resolve(__dirname, './views'),
          to: path.resolve(__dirname, './dist/views')
        },
        {
          from: path.resolve(__dirname, './keyOpenSSL'),
          to: path.resolve(__dirname, './dist/keyOpenSSL')
        },
        {
          from: path.resolve(__dirname, 'package.json'),
          to: path.resolve(__dirname, './dist/package.json')
        },
        {
          from: path.resolve(__dirname, 'package-lock.json'),
          to: path.resolve(__dirname, './dist/package-lock.json')
        },
        {
          from: path.resolve(__dirname, 'serve_bin'),
          to: path.resolve(__dirname, './dist/serve_bin')
        },
        {
          from: path.resolve(__dirname,'README.md'),
          to: path.resolve(__dirname, './dist/README.md')
        }
      ],
    }),
    new DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    })
  ],
  resolve: {
    extensions: ['.js', '.json', '.jsx']
  }
}